//
//  ZBListViewController.m
//  ZBCooking
//
//  Created by xzb on 2019/2/18.
//  Copyright © 2019 xzb. All rights reserved.
//

#import "ZBListViewController.h"

@interface ZBListViewController ()
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSDictionary *info;
@end

@implementation ZBListViewController

- (BOOL)canOpenControllerWithParams:(NSDictionary *)params
{
    NSDictionary *info = params[ZBRouterParameterUserInfo];
    if (![info.allKeys containsObject:@"item"]) {
        Log(@"item is empty");
        return NO;
    }
    self.info = info[@"item"];
    return YES;
}

#pragma mark - life cycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = [self.info zb_safeStringValueForKey:@"title" defaultValue:@""];
}

#pragma mark - api

#pragma mark - Delegete

#pragma mark - Event Response

#pragma mark - privite

- (void)ZB_addSubView
{
    [super ZB_addSubView];
    
    [self.view addSubview:self.collectionView];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(self.navigationBarBottom);
        make.left.right.offset(0);
        make.bottom.offset(0);
    }];
}

- (void)ZB_sendQuery
{
    [super ZB_sendQuery];
    [self updateData];
}

- (void)updateData
{
    NSArray *list = [self.info zb_safeArrayValueForKey:@"sub_items" defaultValue:@[]];
    [self.collectionView zb_setup:^(ZBCollectionMaker *maker) {
        maker.section(@"<section-menu");
        for (NSDictionary *item in list) {
            maker.row(@"<cell-item>").model(item);
        }
    }];
}

#pragma mark - delegete

#pragma mark - notification

#pragma mark - kvo

#pragma mark - getter And setter

- (UICollectionView *)collectionView
{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.minimumInteritemSpacing = 10;
        layout.minimumLineSpacing = 10;
        layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
        
        _collectionView = [UICollectionView collectionViewWithLayout:layout];
        /*
        @weakify(self);
        [self.collectionView zb_blackHeaderRefreshingBlcok:^{
            @strongify(self);
            [self ZB_sendQuery];
        }];
        MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            @strongify(self);
            [self.collectionView.mj_footer endRefreshing];
            [self.collectionView.mj_footer resetNoMoreData];
        }];
       
        [footer setTitle:@"已经到底了" forState:1];
        self.collectionView.mj_footer = footer;
          */
        [_collectionView zb_configTableView:^(ZBCollectionProtoFactory *config) {
            config.cell(@"<cell-item>").cls(@"ZBItemCell").nibName(@"ZBItemCell");
            config.cell(@"<cell-space>").cls(@"ZBSpaceCell");
        }];
    }
    return _collectionView;
}

@end
